
#Newimglist=`echo "$HOME/data/history/gft_observed.list"`
Newimglist=`echo "$HOME/data/history/Y2020/gft_observed_200627.list"`
stringtimeForMonitorT=`date -u +%Y%m%d`
dir_reduction=$HOME/workspace/reduction/$stringtimeForMonitorT
cd $HOME/workspace/reduction

if test ! -s $stringtimeForMonitorT
then
    echo "making $stringtimeForMonitorT"
    mkdir $stringtimeForMonitorT
fi
cd $dir_reduction
mkdir bias
mkdir dark
mkdir flat
mkdir object
mkdir calibration
Dir_bias=`echo $dir_reduction"/"bias`
Dir_dark=`echo $dir_reduction"/"dark`
Dir_flat=`echo $dir_reduction"/"flat`
Dir_object=`echo $dir_reduction"/"object`
Dir_calibration=`echo $dir_reduction"/"calibration`

xbiascheckandcombine (  )
{
cd $Dir_bias
if test ! -r Zero.fit
then
	nimage=`echo bias*.fit | wc | awk '{print($1)}'`
	if [ $nimage -ge 5  ]
	then
       		 echo *.fit | xargs -n 1 | grep  "bias" >aaa
       		 if test -s aaa
       		 then
       		         ls bias*.fit >listbias
       		         xbiascom.sh listbias
       		         cp Zero.fit ../calibration
       		         cd ../
       		 fi
	else
		sleep 1 
	fi
else
	echo "have Zero.fit"	
fi

}
xdarkcheckandcombine (  )
{
cd $Dir_dark	    
if test ! -r Dark.fit
then
	nimage=`echo dark*.fit | wc | awk '{print($1)}'`
	if [ $nimage -ge 5   ]
	then
        	echo *.fit | xargs -n 1 | grep  "dark" >aaa
        	if test -s aaa
        	then
        	        ls dark*.fit >listdark
        	        if test -s ../calibration/Zero.fit
        	        then
        	                cp ../calibration/Zero.fit ./
        	                xzerocorr_list.sh listdark
        	        fi
        	        rm -rf Dark.fit
        	        xdarkcom.sh listdark
        	        cp Dark.fit ../calibration
        	        cd ../
        	fi
	else
		sleep 1
	fi
else
	echo "have Dark.fit"
fi


}
xflatcheckandcombine (  )
{

cd $Dir_flat
if test ! -r Flat*.fit
then
	nimage=`echo *.fit | wc | awk '{print($1)}'`
	if [ $nimage -ge 5   ]
	then
        	echo * | xargs -n 1 | grep  "flat" >aaa
        	if test -s aaa
        	then
        	        if test -r ../calibration/Zero.fit
        	        then
        	                cp ../calibration/Zero.fit $Dir_flat
        	        fi
        	        if test -r ../calibration/Dark.fit
        	        then
        	                cp ../calibration/Dark.fit $Dir_flat
        	        fi
        	                cd $Dir_flat
        	                ls f*.fit | grep -v "Zero.fit" | grep -v "Dark.fit" >listflat
        	                xzerocorr_list.sh listflat
        	                xdarkcorr_list.sh listflat
        	                xflatcom.sh listflat
        	                Flatimg=`ls Flat*.fit`
        	                if test -s $Flatimg
        	                then
        	                        cp  $Flatimg ../calibration
        	                fi
        	                cd ../
        	                echo "flatcom is finished!"
        	fi
	else
		sleep 1
	fi
else
	echo "have Flat.fit"
fi
echo `pwd`

}

xobjcorrection ( )
{
	echo "=======xobjcorrection========"
	cd $Dir_object
	echo `pwd` 
	echo "===2===fitsname="$fitsname
	sourcename=`gethead $fitsname "OBJECT"`
	echo "===3===sourcename="$sourcename
#	if [ "$sourcename"x == "GRB"x   ]
	#==================for test
	if [ "$sourcename"x == "G0015_28210467"x   ]
	then
		echo "The $fitsname is an image for GRB science"
		sleep 5	
		cp ../calibration/Zero.fit $Dir_object
		cp ../calibration/Dark.fit $Dir_object
		cp ../calibration/Flat*.fit $Dir_object
	#	cd $Dir_object
		ls $fitsname >listdir_real
#		we do not test for this part, xzeroflatcorr_listdir.sh
#		xzeroflatcorr_listdir.sh listdir_real
		xobjectreduction
	else
		echo "The $fitsname is not an image for GRB science"
	fi
	cd $dir_reduction
}


xobjectreduction (  )
{

	echo "=========xobjectreduction========"
	echo $fitsname
    	ls $fitsname >listprocess
	cat listprocess
	echo "========4=========="
	ximagecheck.sh listprocess
	echo `pwd`
	if test -s listaaaa
        then
		echo "have listaaaa"
#                xbmatch_2.sh listaaaa
	else
		echo "no listaaa"
		echo "Bad image for $fitsname"
		echo "over"
	fi
	
#    xbmatch3_listdir.sh listprocess
   # x60cmF_Plus2mass.sh $fitsname
 #  rm -rf /tmp/* 
 # xSendmsg2Wechat12
#  x60cmF_Plus2mass_20181022.sh $fitsname
#     x60cmF_usnob.sh $fitsname
}

ximagetypecheck ( )
{
	echo "ximagetypecheck"
	echo "fitsname="$fitsname
	imgtype=`gethead $fitsname "ccdtype"`
	echo $imgtype
	if [ "$imgtype"x == "BIAS"x ]
	then
		cp $fitsname bias
		xbiascheckandcombine
	elif  [ "$imgtype"x == "DARK"x ]
	then
		cp $fitsname dark
		xdarkcheckandcombine
	elif [ "$imgtype"x == "FLAT"x ]
	then
		cp $fitsname flat
		xflatcheckandcombine
	elif  [ "$imgtype"x == "LIGHT"x ]
	then
		cp $fitsname object
		xobjcorrection
	fi
}


xcopyobslist (  )
{
   # echo "xcopyobslist"
    if test ! -r oldlist
    then
        touch oldlist
    fi
#============
#for test
    rm -rf oldlist
    touch oldlist
#    =============
    if test -s $Newimglist
    then
	echo $Newimglist
	echo "have $Newimglist"
        cp $Newimglist $dir_reduction
        # gft_observed.list is copied
        #cat gft_observed.list | awk '{print($1"/"$2)}' >newlist
	datalist=`echo *.list | head -1`
	echo $datalist
        cat $datalist | column -t | awk '{print($1"/"$2)}' >newlist
        diff oldlist newlist | grep ">" | tr -d '>' | grep -v test_ | column -t  >listmatch
        linedata=`cat listmatch | wc -l`
	echo $linedata
        if [ "$linedata" -ne 0  ]
        then
            echo "new image exits"
            cat listmatch | while read linedataroad
            do
		echo $linedataroad
                N_dataline=`echo $linedataroad | wc | awk '{print($2)}'`
                echo $linedataroad
                echo $N_dataline 
                if [ $N_dataline -lt 1   ]
                then
                    echo "The information of this fits is not correct"
                else
                   # pathroad=`echo $linedataroad | awk '{print($1)}'`
                   # fitsname=`echo $linedataroad | awk '{print($2)}'`
                   # cp $pathroad"/"$fitsname $dir_reduction
			echo "The information is correct"
                        cp $linedataroad $dir_reduction
                        fitsname=`echo $linedataroad | sed 's/\// /g' | tr ' ' '\n' | grep ".fit" | awk '{print($1)}'`
			echo $fitsname
                        echo $linedataroad >>oldlist
			ximagetypecheck
                fi
            done
        else
            sleep 1 
            continue
        fi
    else
        echo "No any data"
        sleep 5
        continue
    fi

}




while :
do
	cd  $dir_reduction
	#xcopyobslist
	echo $Newimglist
	Dir_present=` cat $Newimglist | cut -c 1-34 | awk '{print($1)}' | tail -1`
	echo "Dir_present="$Dir_present
	sleep 3
	xgft_auto.sh $Dir_present
#	echo "The code will sleep for $sleeptime"
	sleep 1
done
